package com.wutao.panzh.index.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.index.UserInfo;
import com.panzh.myconst.UserContst;
import com.panzh.util.StringUtil;
import com.wutao.panzh.service.index.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@Controller
public class UserHandler {

    @Reference
    private UserService userService;

    /**
     * @param userTel：登录所需要的账号，使用的是电话
     * @param loginPwd：登录密码
     * @return 登录成功返回页面，登录失败返回提示
     */
    @ResponseBody
    @RequestMapping("/user/do/login")
    public ResultVO getIndex(@RequestParam("userTel") String userTel,
                             @RequestParam("loginPwd") String loginPwd,
                             HttpServletRequest request) {

        //账号存在，密码不正确的情况下；
        UserInfo userByAcct = userService.getUserByTel(userTel);
        if (userByAcct == null){
            return new ResultVO<String>(ResultVO.FAILED ,UserContst.ACCOUNT_DOES_NOT_EXIST,ResultVO.NO_DATA);
        }

        //对密码进行md5加密；
        loginPwd = StringUtil.md5(loginPwd);

        //取出数据库的原串；
        String sourceLoginPwd = userByAcct.getPasswd();
        if (Objects.equals(sourceLoginPwd,loginPwd)){
            //获取session放入session域
            HttpSession session = request.getSession();
            session.setAttribute(UserContst.USER_INFO_IS_EXIST,userByAcct);
            return new ResultVO<String>(ResultVO.SUCCESS , ResultVO.NO_MSG, ResultVO.NO_DATA);
        }else {
            return new ResultVO<String>(ResultVO.FAILED , UserContst.PASSWORD_ERROR, ResultVO.NO_DATA);
        }
    }

}
